import { _decorator, BoxCollider2D, Component, ERigidBody2DType, Layers, math, Node, RigidBody2D, TiledMap, UITransform, v2, v3 } from 'cc';
const { ccclass, property } = _decorator;

export enum TileMapLayer {
    platformH = "platformH",
    platformV = "platformV"
}

@ccclass('TileMapTools')
export class TileMapTools extends Component {
    
    private selfTiledMap: TiledMap;
    private selfTransform: UITransform;
    private colliderManager: Node;
    start() {
        this.find();
        this.initColliderManager();
        this.creatorTileCollider();
    }

    find() {
        this.selfTiledMap = this.node.getComponent(TiledMap);
        this.selfTransform = this.node.getComponent(UITransform);
    }

    initColliderManager() {
        this.colliderManager = new Node("colliderManager");
        this.colliderManager.layer = Layers.Enum.UI_2D;
        this.colliderManager.addComponent(UITransform);
        this.node.addChild(this.colliderManager);

        const selfSize = this.selfTransform.contentSize;
        const width = selfSize.width / 2;
        const height = selfSize.height / 2;
        const cmPos = v3(width * -1, height * -1)
        this.colliderManager.setPosition(cmPos);
    }

    private itemSize = math.size(54, 54);

    creatorColliderNode(name: string, colliderSize?: math.Size): Node {
        const nd = new Node(name);
        nd.layer = Layers.Enum.UI_2D;
        const trans = nd.addComponent(UITransform);
        trans.setContentSize(this.itemSize);


        if (!colliderSize) {
            colliderSize = this.itemSize;
        }
        const boxColl = nd.addComponent(BoxCollider2D);
        boxColl.size = colliderSize;
        boxColl.apply();


        const rig = nd.addComponent(RigidBody2D);
        rig.type = ERigidBody2DType.Static;
        return nd;
    }

    creatorTileCollider() {
        const hLayer = this.selfTiledMap.getLayer(TileMapLayer.platformH);
        const vLayer = this.selfTiledMap.getLayer(TileMapLayer.platformV);
        console.log("tmLayer", hLayer.getLayerSize());
        console.log("selfTiledMap", this.selfTiledMap);

        const layerSize= hLayer.getLayerSize();
        for (let wIndex = 0; wIndex < layerSize.width; wIndex++) {
            for (let hIndex = 0; hIndex < layerSize.height; hIndex++) {
                const gidat = hLayer.getTileGIDAt(wIndex, hIndex);
                if (gidat) {
                    console.log("gidat", gidat);
                    const indexPos = hLayer.getPositionAt(wIndex, hIndex);
                    // console.log("pos", indexPos);
                    const posX = indexPos.x + this.itemSize.width / 2;
                    const posY = indexPos.y + this.itemSize.height / 2;
                    console.log("pos", "x:", posX, "y:", posY);
                    const nd = this.creatorColliderNode(gidat.toString());
                    nd.setPosition(v3(posX, posY));
                    this.colliderManager.addChild(nd);
                }
            }
        }

        let hList: {wIndex: number, hIndex: number}[] = [];
        for (let hIndex = 0; hIndex < layerSize.height; hIndex++) { 
            for (let wIndex = 0; wIndex < layerSize.width; wIndex++) {
                const gidat = hLayer.getTileGIDAt(wIndex, hIndex);
                if (gidat) { 
                    hList.push({ wIndex: wIndex, hIndex: hIndex });
                } else {
                    if (hList.length) {
                        

                        hList = [];
                    }
                }
            }
        }


    }
}


